﻿/*
 * Created by SharpDevelop.
 * User: Vivek
 * Date: 9/4/2009
 * Time: 1:21 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using GxcToCsharpParser.TreeWalkers;
using GxcToCsharpParser.TreeWalkers.ControlFlowGraphBuilder;
namespace Gxc2CsharpConsonle
{
	class Program
	{
      static int nComplexGxs;
      static int nParsedGxs;
      static int nParsedGrcs;
      public static void Main(string[] args)
		{
         //ControlFlowWalker cfg = new ControlFlowWalker();
       //  cfg.GraphConcepts();
         //nComplexGxs = 0;
         //nParsedGxs = 0;
         //nParsedGrcs = 0; 
         //BrowseAllGXs(args[0]);
         SingleFileTest(@"..\..\..\gxc2csharpTranslator.Tests\data\testsimple.gxc");


        
         
		}

      private static void SingleFileTest(string gxcFilePath)
      {
         string filename = Path.GetFileNameWithoutExtension(gxcFilePath);
         Gxc2CsharpBuilder builder = new Gxc2CsharpBuilder();
         Antlr.Runtime.Tree.CommonTree ast = builder.ParseGxcFileOnly(gxcFilePath);
        
       //  WriteParseTreeDot(filename, ast);
         //LinearControlFlowBuilder linearCfg = new LinearControlFlowBuilder();
         //var linearFlow = linearCfg.Build(ast);
         //linearCfg.WriteToFile("linearCfg.txt");
         //ControlFlowGraphBuilder cfgBuilder = new ControlFlowGraphBuilder();
         //var cfg = cfgBuilder.Build(linearFlow);
         //var cfgRederer = new GxcToCsharpParser.Utility.QuickGraphCfgDotEngine();
         //cfgRederer.Render(cfg, "graph");

      }

      private static void WriteParseTreeDot(string filename, Antlr.Runtime.Tree.CommonTree ast)
      {
         Antlr.Utility.Tree.DOTTreeGenerator dot = new Antlr.Utility.Tree.DOTTreeGenerator();
         
         var st = dot.ToDOT(ast);
         
         System.IO.File.WriteAllText(filename + ".dot", st.ToString());
         GxcToCsharpParser.Utility.DotToImageEngine.GenereateImageFromDotFile(filename + ".dot");
      }
      private static void BrowseAllGXs(string dirPath)
      {

         Directory.CreateDirectory(@".\temp");
         DateTime start = DateTime.Now;
         ProcessSubDirs(dirPath);
         DateTime end = DateTime.Now;
         TimeSpan span = end - start;

         Console.WriteLine("-----------------------------------------");
         Console.WriteLine("Time taken for parsing = {0}", span.ToString());
         Console.WriteLine("Number of parsed gx's = {0}", nParsedGxs);
         Console.WriteLine("Number of parsed grc's = {0}", nParsedGrcs);
         Console.WriteLine("Number of complex gx's = {0}", nComplexGxs);
       
         


      }
     private static void ProcessSubDirs(string dirPath)
      {

         var gxcFiles = new DirectoryInfo(dirPath).GetFiles("*.gxc");
         ProcessFiles(gxcFiles);
         var subDirs = new DirectoryInfo(dirPath).GetDirectories();
         foreach (var subDir in subDirs)
         {
            ProcessSubDirs(subDir.FullName); 
            
         }
      }
      private static void ProcessFiles(FileInfo[] gxcFiles)
      {
         foreach (var gxcFile in gxcFiles)
         {
            string grcFile = Path.GetFileNameWithoutExtension(gxcFile.Name) + ".grc";
            try
            {
               Console.WriteLine("");
               Console.WriteLine("");
               Console.WriteLine("---------------------------------------------------------------");
               Console.WriteLine("Parsing {0}", gxcFile.Name);
               Console.WriteLine("---------------------------------------------------------------");

               Gxc2CsharpBuilder builder = new Gxc2CsharpBuilder();
               builder.Build(gxcFile.FullName, Path.GetDirectoryName(gxcFile.FullName)+"\\"+grcFile);
               Console.WriteLine("---- Parsed {0} {1} is complex gx {2}", gxcFile, grcFile, builder.GrcParser.IsComplexGrcFile);
               builder.Stats();

               if (builder.GrcParser.HasCompiled)
                  nParsedGrcs++;
               if (builder.GrcParser.IsComplexGrcFile)
                  nComplexGxs++;
               nParsedGxs++;
            }
            catch (Exception e)
            {
               Console.WriteLine(e.Message);
               Console.WriteLine(e.StackTrace);;
            }


         }
      }
     
	}
}